//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// 'Postal address items' dialog, 'Highways' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "PAItems.h"
# include "ListView.h"
# include "Map.h"
# include "ChangePAI.h"
# include "EditText.h"
# include "PropertyPages.h"
# include "Globals.h"

class CCreateHighway : public CDialog {
  public:
	CCreateHighway () : CDialog (IDD_CREATE_PAI) {}

	// Override.
	virtual bool OnInit (HWND _hwndFocus) {
		SetWindowText (m_hWnd, "Add Highway");

		EnableDlgItem (IDOK, false);

		// Static.
		SetDlgItemText (IDC_PARENT_ITEMS, "Regions:");

		// Regions.
		m_List.Init (m_hWnd, IDC_LIST, "Regions");
		InitRegionsList (m_List);
		FillRegionsList (m_List);
		SetSelection (m_List, 0);

		return true;
	}
	virtual void OnDestroy () {
		m_List.Destroy ();
	}

	virtual void OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
		switch (_wCmd) {
			case IDC_TEXT: {
				if (_wNotifyCode == EN_CHANGE)
					EnableDlgItem (IDOK, true);
				return;
			}

			case IDOK: {
				const HWND hText = GetDlgItem (IDC_TEXT);

				// Get text.
				const size_t cLen = ::GetWindowTextLength (hText);
				char * const strName = reinterpret_cast<char *> (alloca (cLen + 1));
				::GetWindowText (hText, strName, cLen + 1);

				// Get country.
				const size_t cSel = ListView_GetSelectionMark (m_List);
				if (cSel == -1)
					break;
				region_t * const pRegion = reinterpret_cast<region_t *> (GetLParam (m_List, cSel));

				// Create new highway.
				AddHighway (strName, pRegion);

				EndDialog (0);
				return;
			}

			case IDCANCEL:
				EndDialog (1);
				return;
		}
	}

	virtual bool OnNotify (int _idCtrl, NMHDR * _pInfo) {
		switch (_pInfo->code) {
			case NM_RCLICK:       m_List.PopupMenu_OnHeaderRClick (_pInfo); break;
			case LVN_COLUMNCLICK: m_List.Sort_OnColumnClick       (_pInfo); break;
		}
		return false;
	}

  private:
	CListView m_List;
};

CHighwaysPage::CHighwaysPage () : CPropertyPages::CPage (IDD_PA_HIGHWAYS) {}

void CHighwaysPage::_FillHighways (highway_t * _pHighway) {
	ListView_DeleteAllItems (m_List);

	CCritSec::CLock lock (g_cs);

	size_t cItem = 0;
	for (highways_t::iterator i = g_map.Highways.begin (); i != g_map.Highways.end (); ++ i) {
		const highway_t & highway = * i;
		if (highway.IsDeleted ())
			continue;

		InsertItem     (m_List, cItem, reinterpret_cast<LPARAM> (& highway), "%s", OutputPAString (highway.strName));
		SetSubItemText (m_List, cItem, 1, "%s", OutputPAString (highway.pRegion->strName));
		SetSubItemText (m_List, cItem, 2, "%s", OutputPAString (highway.pRegion->pCountry->strName));

		if (& highway == _pHighway)
			SetSelection (m_List, cItem);

		++ cItem;
	}

	// Set the best width.
	AutoSizeColumns (m_List);

	m_List.Sort ();
}

void CHighwaysPage::_UpdateTotalHighways () {
	size_t cHighways = 0;
	for (highways_t::iterator i = g_map.Highways.begin (); i != g_map.Highways.end (); ++ i) {
		const highway_t & hw = * i;
		if (hw.IsDeleted ())
			continue;

		++ cHighways;
	}

	char strTotal [32];
	::sprintf (strTotal, "Total: %d", cHighways);

	SetDlgItemText (IDC_TOTAL, strTotal);
}

bool CHighwaysPage::OnInit (HWND _hwndFocus) {
	// Buttons.
	EnableDlgItem (IDC_REMOVE, false);
	EnableDlgItem (IDC_RENAME, false);

	// Highways.
	m_List.Init (m_hWnd, IDC_LIST, "Highways");
	AddColumn (m_List, 0, "Highway");
	AddColumn (m_List, 1, "Region");
	AddColumn (m_List, 2, "Country");
	m_List.SetExtStyle (LVS_EX_FULLROWSELECT);

	return true;
}

void CHighwaysPage::OnDestroy () {
	m_List.Destroy ();
}

void CHighwaysPage::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_ADD: {
			if (g_map.Regions.empty ()) {
				WarningMsgBox ("Please create regions first.");
				break;
			}

			CCreateHighway chd;
			if (chd.ShowModal (m_hWnd) == 0) {
				_FillHighways (& g_map.Highways.back ());
				_UpdateTotalHighways ();
			}

			return;
		}

		case IDC_RENAME: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			if (cSel == -1)
				break;

			highway_t * const pHighway = reinterpret_cast<highway_t *> (GetLParam (m_List, cSel));

			string_t strName = pHighway->strName;
			if (EditText (m_hWnd, strName, "Edit highway", false)) {
				RenamePAI (pHighway, strName);
				SetItemText (m_List, cSel, strName.c_str ());
			}

			return;
		}

		case IDC_REMOVE: {
			EnableDlgItem (IDC_RENAME, false);
			EnableDlgItem (IDC_REMOVE, false);

			const size_t cSel = ListView_GetSelectionMark (m_List);
			if (cSel == -1)
				break;

			highway_t * const pHighway = reinterpret_cast<highway_t *> (GetLParam (m_List, cSel));
			DeletePAI (pHighway);
			_FillHighways ();
			_UpdateTotalHighways ();

			return;
		}
	}
}

bool CHighwaysPage::OnNotify (int _idCtrl, NMHDR * _pInfo) {
	switch (_pInfo->code) {
		case NM_RCLICK:
			m_List.PopupMenu_OnHeaderRClick (_pInfo);
			return false;

		case LVN_COLUMNCLICK:
			m_List.Sort_OnColumnClick (_pInfo);
			break;

		case PSN_SETACTIVE: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			highway_t * const pHighway = cSel == -1 ? NULL : reinterpret_cast<highway_t *> (GetLParam (m_List, cSel));
			_FillHighways (pHighway);
			_UpdateTotalHighways ();
		}
		case NM_CLICK:
		case LVN_KEYDOWN: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			EnableDlgItem (IDC_RENAME, cSel != -1);
			EnableDlgItem (IDC_REMOVE, cSel != -1);
			break;
		}
	}

	return CPage::OnNotify (_idCtrl, _pInfo);
}
